package com.example.demo.service.ThreadPool.impl;

import com.example.demo.service.ThreadPool.AsyncService;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author yzh
 * 线程池Impl,将线程任务写在这，比如F1,F2,在controller调用时会多线程执行
 * @date 2022/11/21
 */
@Service
public class AsyncServiceImpl implements AsyncService {
    private static final Logger logger = LoggerFactory.getLogger(AsyncServiceImpl.class);


    @Override
    @Async("taskExecutor")//线程池名称
    public void executeAsync() {
        logger.info("start executeAsync");

        System.out.println("异步线程要做的事情");
        System.out.println("可以在这里执行批量插入等耗时的事情");

        logger.info("end executeAsync");

    }


    @Override
    @Async("taskExecutor")
    public void F1() throws InterruptedException {
        System.out.println(Thread.currentThread().getName());
        for (int i = 0; i < 10; i++) {
            System.out.println("A-" + i);
            Thread.sleep(500);
        }

    }

    @Override
    @Async("taskExecutor")
    public void F2() throws InterruptedException {
        System.out.println(Thread.currentThread().getName());
        for (int i = 20; i < 30; i++) {
            System.out.println("B-" + i);
            Thread.sleep(500);
        }

    }
}
